const { BrowserWindow, screen, ipcMain } = require('electron')
const path = require('path')
const mEmitter = require('../Manager/eventEmitter');

let mainWindow;
function createMainWindow() {
    //创建窗口
    mainWindow = new BrowserWindow({
        x: screen.getPrimaryDisplay().workArea.width / 2-300,
        y: screen.getPrimaryDisplay().workArea.width / 2-250,
        width: 600,
        height: 500,
        alwaysOnTop: true,
        show: false,
        autoHideMenuBar: true,
        backgroundColor: '#f0ffff',
        title: "计调辅助工具",
        webPreferences: {
            preload: path.join(__dirname, './script/index_preload.js')
        }

    })

    //载入主界面
    mainWindow.loadFile(path.join(__dirname, 'index.html'))

    mainWindow.once("ready-to-show", () => {
        mainWindow.show();
    })

    process_IPC();
}


//******************进程间通信****************
function process_IPC() {
    //显示主界面
    mEmitter.on('displayMainWindow', () => {
        mainWindow.show();
    })

    //打开抢票功能界面
    ipcMain.on('openTicketGrabbing', () => {
        require('../Module_GrabTicket/grabTicket').createOptionWindow();
    })
    //打开自动生成二维码界面
    ipcMain.on('openAutomaticallyGenerateQRcode', () => {
        // var createQR_Window;
        // if (!createQR_Window) {
        mainWindow.hide();
        require('../Module_CreatRouteQR/createRouteQR').createWindow();
    })
    //打开自动生成线路ID界面
    ipcMain.on('openAutomaticallyGenerateRouteID', () => {
        mainWindow.hide();
        require('../Module_CreateRouteID/createRouteID').createOptionWindow();
    })
}



module.exports = {
    createMainWindow,
}